package com.qianchang.microfinance.widgets;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.TextView;

import com.dm.library.utils.FormatUtil;
import com.dm.library.widgets.OneLineLinearLayout;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.BankCardBean;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  选择 拍照 相片
 * Created by tangjian on 2016/11/22.
 */

public class SelectPayBackDialog extends Dialog {
    @BindView(R.id.oll_need_pay)
    OneLineLinearLayout ollNeedPay;
    private Context context;
    @BindView(R.id.tv_bank_name)
    TextView tvBankName;
    @BindView(R.id.tv_bank)
    RadioButton tvBank;
    @BindView(R.id.tv_balance)
    RadioButton tvBalance;
    private View view;

    private static final int PAY_BANK = 7;
    private static final int PAY_BALANCE = 6;
    private int payMethod = 0;
    private OnViewClickListener mListener;


    @SuppressWarnings("deprecation")
    public SelectPayBackDialog(Activity context) {
        super(context, R.style.customDialog);
        this.context = context;
        view = LayoutInflater.from(context).inflate(R.layout.select_payback_dialog, null);
        ButterKnife.bind(this, view);
        this.setContentView(view);
        this.setCancelable(true);
        Window dialogWindow = this.getWindow();
        if (null != dialogWindow) {
            dialogWindow.setGravity(Gravity.BOTTOM);
            dialogWindow.setWindowAnimations(R.style.AnimBottom);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = context.getWindowManager().getDefaultDisplay().getWidth();// 宽度全屏
            dialogWindow.setAttributes(lp);
        }
        onPayMethodChanged(payMethod);
    }


    @OnClick({R.id.iv_dismiss, R.id.rl_bank_pay, R.id.rl_balance_pay, R.id.btn_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_dismiss:
                dismiss();
                break;
            case R.id.rl_bank_pay:
                payMethod = PAY_BANK;
                onPayMethodChanged(payMethod);
                break;
            case R.id.rl_balance_pay:
                payMethod = PAY_BALANCE;
                onPayMethodChanged(payMethod);
                break;
            case R.id.btn_confirm:
                notifyClick(payMethod);
                dismiss();
                break;
        }
    }

    public void onPayMethodChanged(int payMethod) {
        switch (payMethod) {
            case PAY_BANK:
                tvBank.setChecked(true);
                tvBalance.setChecked(false);
                break;
            case PAY_BALANCE:
                tvBank.setChecked(false);
                tvBalance.setChecked(true);
                break;
        }
    }

    public void setOnViewClickListener(OnViewClickListener listener) {
        mListener = listener;
    }

    public void setBalanceStatues(int statues) {
        if (0 == statues) {
            tvBalance.setText("余额不足");
        }
    }

    public void setBalanceEnough(boolean isEnough) {
        View v = view.findViewById(R.id.rl_balance_pay);
        tvBalance.setEnabled(isEnough);
        v.setEnabled(isEnough);
        if (isEnough) {
            tvBalance.setText("");
            tvBalance.setCompoundDrawablesWithIntrinsicBounds(null, null, context.getResources().getDrawable(R.drawable.selector_cb_protocol), null);
            tvBalance.setChecked(true);
            tvBank.setChecked(false);
            payMethod = PAY_BALANCE;
        } else {
            tvBalance.setText("余额不足");
            tvBalance.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
            tvBalance.setChecked(false);
            tvBank.setChecked(true);
            payMethod = PAY_BANK;
        }
    }

    public void setBankCard(BankCardBean bankCardBean) {
        try {
            int length = bankCardBean.getBankCardNumber().length();
            String str = bankCardBean.getBankName() + "(" + bankCardBean.getBankCardNumber().substring(length - 4, length) + ")";
            tvBankName.setText(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public interface OnViewClickListener {
        void onSelectClick(int payMethod);
    }

    public void notifyClick(int payMethod) {
        if (null != mListener) {
            mListener.onSelectClick(payMethod);
        }
    }

    public void setNeedPay(double money) {
        ollNeedPay.setRightContent(FormatUtil.numKbPointFormat(money) + "元");
    }

}
